Overwrite to-be-installed source file (without overwriting the orignal file in the source directory)
See original GitHub issueI want to encode the git hash of the lastest commit at the time of installing as the output of a function in my library.
I am using a structure
.git/
setup.py
|- mylib/
| - __init__.py
| - git.py
whereby git.py
contains a dummy, unconfigured, function
def git():
return None
At the time of installing I want to overwrite git.py
such that it returns the hash of the latest git commit, e.g.:
def git():
return "2d04d1a10f81d24183df7622c95398d60106dfff"
(whereby the hash is the output of git rev-parse HEAD
at the time of installing).
So my question:
How can I overwrite the to-be-installed git.py
, using setuptools, without overwriting the file in the source directory?
Current work-around setup.py
import subprocess
git_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).strip().decode('UTF-8')
cmd = '''def git():
return "{0:s}"
'''.format(git_hash)
os.rename('mylib/git.py', 'mylib/git.py.bak')
with open('mylib/git.py', 'w') as f:
f.write(cmd)
setup(
name = 'mylib',
version = __version__,
# ...
packages = find_packages(),
)
os.rename('mylib/git.py.bak', 'mylib/git.py')
Which has the down-sight of temporarily changing git.py
in the source directory. I’d rather leave that file completely untouched.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Move or copy without overwrite and check success
I.e. create an empty file first using this technique. If that succeeds, you can then overwrite the empty file. Similarly for python.
Read more >How can I write a value to a file without it being overwritten ...
I have a script that I pass a different input and get a different output every time, so I want to write all...
Read more >How to Force cp Command to Overwrite without Confirmation
In this guide, we will show how to force the cp command to overwrite a copy operation without confirmation in Linux.
Read more >cp without overwriting destination permissions - Server Fault
The next will not overwrite the file permissions and ownership + groupship: cp --no-preserve=mode,ownership /tmp/file /home/file.
Read more >Linux cp command help and examples - Computer Hope
Make a backup of each existing destination file that would otherwise be overwritten or removed. The control parameter specifies what version ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Very clear, thanks once more!
It is based on Git tags. This plugin makes sure that the version is sourced from a file that you’re supposed to commit. It is a template git fills out when generating a git archive. This only works for tagged commits only, though. When running from a normal clone, it still uses the current repo.