Skip to content

Commit

Permalink
sync: make sync usable from outside ohmu_common_py directory
Browse files Browse the repository at this point in the history
And don't resync files with no changes plus log what we do.
  • Loading branch information
saaros committed May 27, 2016
1 parent c3cc48f commit 5e883ef
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
See LICENSE for details
"""

import logging
import os
import version
import sys
Expand All @@ -19,17 +20,42 @@
]


def main(target):
ver = version.get_project_version("ohmu_common_py/version.py")
curdir = os.path.dirname(__file__)
def sync_files(source_dir, target_name, target_dir=None, common_dir=None, test_dir=None):
ver = version.get_project_version(os.path.join(os.path.dirname(__file__), "ohmu_common_py/version.py"))
for src_f in FILES:
with open(os.path.join(curdir, src_f), "r") as fp:
with open(os.path.join(source_dir, src_f), "r") as fp:
source = fp.read()
dst_f = src_f.replace("ohmu_common_py", target)
dst = os.path.join(curdir, "..", target, dst_f)
source = source.replace("ohmu_common_py", target_name)
if target_dir:
dst = os.path.join(target_dir, src_f.replace("ohmu_common_py", target_name))
elif src_f.startswith("ohmu_common_py/") and common_dir:
dst = os.path.join(common_dir, src_f.replace("ohmu_common_py/", ""))
elif src_f.startswith("test/") and test_dir:
dst = os.path.join(test_dir, src_f.replace("test/", ""))
else:
logging.info("%r: skipping", src_f)
continue

# check existing file for changes
if os.path.exists(dst):
with open(dst, "r") as fp:
existing_data = fp.read()
existing_data = "\n".join(existing_data.splitlines()[1:]) + "\n"
if existing_data == source:
logging.info("%r: no update required", dst)
continue
with open(dst, "w") as fp:
fp.write("# Copied from https://github.com/ohmu/ohmu_common_py {} version {}\n".format(src_f, ver))
fp.write(source.replace("ohmu_common_py", target))
fp.write(source)
logging.info("%r: UPDATED", dst)


def main(target):
from ohmu_common_py import logutil
logutil.configure_logging()
curdir = os.path.dirname(__file__)
target_dir = os.path.join(curdir, "..", target)
sync_files(source_dir=curdir, target_name=target, target_dir=target_dir)


if __name__ == "__main__":
Expand Down

0 comments on commit 5e883ef

Please sign in to comment.