Repository: radiasoft/pykern

Issue:
Add PYKERN_PKSETUP_NO_VERSION=1
In order to install a repo directly from a GitHub repo download zip (no .git or egg-info), need to override version.

Discussion:

Patch:
diff --git a/pykern/pksetup.py b/pykern/pksetup.py
index 9a32644..f86a1a0 100644
--- a/pykern/pksetup.py
+++ b/pykern/pksetup.py
@@ -66,6 +66,8 @@ TESTS_DIR = "tests"
 
 _VERSION_RE = r"(\d{8}\.\d+)"
 
+_cfg = None
+
 
 class NullCommand(distutils.cmd.Command, object):
     """Use to eliminate a ``cmdclass``.
@@ -689,6 +691,14 @@ def _version(base):
         str: Chronological version "yyyymmdd.hhmmss"
         str: git sha if available
     """
+    from pykern import pkconfig
+
+    global _cfg
+
+    if not _cfg:
+        _cfg = pkconfig.init(no_version=(False, bool, "use utcnow as version"))
+    if _cfg.no_version:
+        return _version_from_datetime(), None
     v1 = _version_from_pkg_info(base)
     v2, sha = _version_from_git(base)
     if v1:
@@ -706,6 +716,15 @@ def _version_float(value):
     return m.group(1)[: -len(m.group(2))] if m.group(2) else m.group(1)
 
 
+def _version_from_datetime(value=None):
+    # Avoid 'UserWarning: Normalizing' by setuptools
+    return str(
+        pkg_resources.parse_version(
+            (value or datetime.datetime.utcnow()).strftime("%Y%m%d.%H%M%S"),
+        ),
+    )
+
+
 def _version_from_git(base):
     """Chronological version string for most recent commit or time of newer file.
 
@@ -726,15 +745,13 @@ def _version_from_git(base):
     # Under development?
     sha = None
     if len(_git_ls_files(["--modified", "--deleted"])):
-        vt = datetime.datetime.utcnow()
+        vt = None
     else:
         branch = _check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).rstrip()
         vt = _check_output(["git", "log", "-1", "--format=%ct", branch]).rstrip()
         vt = datetime.datetime.fromtimestamp(float(vt))
         sha = _check_output(["git", "rev-parse", "HEAD"]).rstrip()
-    v = vt.strftime("%Y%m%d.%H%M%S")
-    # Avoid 'UserWarning: Normalizing' by setuptools
-    return str(pkg_resources.parse_version(v)), sha
+    return _version_from_datetime(vt), sha
 
 
 def _version_from_pkg_info(base):


Test patch:
diff --git a/tests/pksetup_test.py b/tests/pksetup_test.py
index feb51af..7e5339c 100644
--- a/tests/pksetup_test.py
+++ b/tests/pksetup_test.py
@@ -5,7 +5,7 @@
 :license: http://www.apache.org/licenses/LICENSE-2.0.html
 """
 from __future__ import absolute_import, division, print_function
-from subprocess import check_call, call
+import subprocess
 import contextlib
 import glob
 import os
@@ -27,26 +27,26 @@ def test_build_clean():
     from pykern import pkunit
 
     with _project_dir("pksetupunit1") as d:
-        check_call(["python", "setup.py", "sdist", "--formats=zip"])
+        subprocess.check_call(["python", "setup.py", "sdist", "--formats=zip"])
         archive = _assert_members(
             ["pksetupunit1", "package_data", "data1"],
             ["scripts", "script1"],
             ["examples", "example1.txt"],
             ["tests", "mod2_test.py"],
         )
-        check_call(["python", "setup.py", "build"])
+        subprocess.check_call(["python", "setup.py", "build"])
         dat = os.path.join("build", "lib", "pksetupunit1", "package_data", "data1")
         assert os.path.exists(
             dat
         ), "When building, package_data should be installed in lib"
         bin_dir = "scripts-{}.{}".format(*(sys.version_info[0:2]))
-        check_call(["python", "setup.py", "test"])
+        subprocess.check_call(["python", "setup.py", "test"])
         assert os.path.exists("tests/mod2_test.py")
-        check_call(["git", "clean", "-dfx"])
+        subprocess.check_call(["git", "clean", "-dfx"])
         assert not os.path.exists(
             "build"
         ), "When git clean runs, build directory should not exist"
-        check_call(["python", "setup.py", "sdist"])
+        subprocess.check_call(["python", "setup.py", "sdist"])
         pkio.unchecked_remove(archive)
         _assert_members(
             ["!", "tests", "mod2_work", "do_not_include_in_sdist.py"],
@@ -54,10 +54,10 @@ def test_build_clean():
         )
         # TODO(robnagler) need another sentinel here
         if os.environ.get("PKSETUP_PKDEPLOY_IS_DEV", False):
-            check_call(["python", "setup.py", "pkdeploy"])
+            subprocess.check_call(["python", "setup.py", "pkdeploy"])
 
 
-def not_a_test_optional_args():
+def test_optional_args():
     """Create a normal distribution
 
     Installs into the global environment, which messes up pykern's install.
@@ -66,12 +66,46 @@ def not_a_test_optional_args():
     from pykern import pkio
     from pykern import pksetup
     from pykern import pkunit
+    from pykern import pkdebug
+
+    def _results(pwd, expect):
+        x = d.dirpath().listdir(fil=lambda x: x.basename != d.basename)
+        pkunit.pkeq(1, len(x))
+        a = list(pkio.walk_tree(x[0], "/(civicjson.py|adhan.py|pksetup.*METADATA)$"))
+        pkunit.pkeq(expect, len(a))
+        for f in a:
+            if f.basename == "METADATA":
+                return x[0], f
+        pkunit.pkfail("METADATA not found in files={}", a)
 
     with _project_dir("pksetupunit2") as d:
-        call(["pip", "uninstall", "-y", "shijian"])
-        call(["pip", "uninstall", "-y", "adhan"])
-        check_call(["pip", "install", "-e", ".[all]"])
-        check_call(["python", "setup.py", "test"])
+        # clean the work dir then run afind
+        subprocess.check_call(
+            [
+                "pip",
+                "install",
+                "--root",
+                pkunit.work_dir(),
+                # No -e or it will modify global environment
+                ".[all]",
+            ],
+        )
+        x, m1 = _results(d, 3)
+        pkio.unchecked_remove(x, ".git")
+        e = os.environ.copy()
+        e["PYKERN_PKSETUP_NO_VERSION"] = "1"
+        subprocess.check_call(
+            [
+                "pip",
+                "install",
+                "--root",
+                pkunit.work_dir(),
+                ".",
+            ],
+            env=e,
+        )
+        _, m2 = _results(d, 1)
+        pkunit.pkne(m1, m2)
 
 
 @contextlib.contextmanager
@@ -92,12 +126,12 @@ def _project_dir(project):
     d = pkunit.empty_work_dir().join(project)
     pkunit.data_dir().join(d.basename).copy(d)
     with pkio.save_chdir(d):
-        check_call(["git", "init", "."])
-        check_call(["git", "config", "user.email", "pip@pykern.org"])
-        check_call(["git", "config", "user.name", "pykern"])
-        check_call(["git", "add", "."])
+        subprocess.check_call(["git", "init", "."])
+        subprocess.check_call(["git", "config", "user.email", "pip@pykern.org"])
+        subprocess.check_call(["git", "config", "user.name", "pykern"])
+        subprocess.check_call(["git", "add", "."])
         # Need a commit
-        check_call(["git", "commit", "-m", "n/a"])
+        subprocess.check_call(["git", "commit", "-m", "n/a"])
         yield d