question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Overwrite to-be-installed source file (without overwriting the orignal file in the source directory)

See original GitHub issue

I 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.

See also: https://stackoverflow.com/questions/66265283/overwrite-to-be-installed-source-file-without-overwriting-the-orignal-file-in-t

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tdegeuscommented, Feb 21, 2021

Very clear, thanks once more!

1reaction
webknjazcommented, Feb 21, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found