1
0
mirror of https://github.com/facebook/proxygen.git synced 2025-08-07 07:02:53 +03:00

Add annotations to opensource/fbcode_builder

Reviewed By: shannonzhu

Differential Revision: D34224272

fbshipit-source-id: 52e19886ab3d4fb015a557244660dd4357a35c17
This commit is contained in:
Pyre Bot Jr
2022-02-14 16:21:28 -08:00
committed by Facebook GitHub Bot
parent 6c86c07528
commit e77a9fe43a
20 changed files with 235 additions and 208 deletions

View File

@@ -50,7 +50,7 @@ class ChangeStatus(object):
and whether we might need to reconfigure the build system.
"""
def __init__(self, all_changed=False):
def __init__(self, all_changed: bool = False) -> None:
"""Construct a ChangeStatus object. The default is to create
a status that indicates no changes, but passing all_changed=True
will create one that indicates that everything changed"""
@@ -61,7 +61,7 @@ class ChangeStatus(object):
self.source_files = 0
self.make_files = 0
def record_change(self, file_name):
def record_change(self, file_name) -> None:
"""Used by the shipit fetcher to record changes as it updates
files in the destination. If the file name might be one used
in the cmake build system that we use for 1st party code, then
@@ -81,14 +81,14 @@ class ChangeStatus(object):
elif "/fbcode_builder/" not in file_name:
self.source_files += 1
def sources_changed(self):
def sources_changed(self) -> bool:
"""Returns true if any source files were changed during
an update operation. This will typically be used to decide
that the build system to be run on the source dir in an
incremental mode"""
return self.source_files > 0
def build_changed(self):
def build_changed(self) -> bool:
"""Returns true if any build files were changed during
an update operation. This will typically be used to decidfe
that the build system should be reconfigured and re-run
@@ -102,7 +102,7 @@ class Fetcher(object):
extracted data resides and reports this to the consumer via
its `get_src_dir` method."""
def update(self):
def update(self) -> ChangeStatus:
"""Brings the src dir up to date, ideally minimizing
changes so that a subsequent build doesn't over-build.
Returns a ChangeStatus object that helps the caller to
@@ -110,12 +110,12 @@ class Fetcher(object):
the update."""
return ChangeStatus()
def clean(self):
def clean(self) -> None:
"""Reverts any changes that might have been made to
the src dir"""
pass
def hash(self):
def hash(self) -> None:
"""Returns a hash that identifies the version of the code in the
working copy. For a git repo this is commit hash for the working
copy. For other Fetchers this should relate to the version of
@@ -128,7 +128,7 @@ class Fetcher(object):
"""
pass
def get_src_dir(self):
def get_src_dir(self) -> None:
"""Returns the source directory that the project was
extracted into"""
pass
@@ -141,13 +141,13 @@ class LocalDirFetcher(object):
This fetcher cannot update or track changes. It always reports that the
project has changed, forcing it to always be built."""
def __init__(self, path):
def __init__(self, path) -> None:
self.path = os.path.realpath(path)
def update(self):
def update(self) -> ChangeStatus:
return ChangeStatus(all_changed=True)
def hash(self):
def hash(self) -> str:
return "0" * 40
def get_src_dir(self):
@@ -155,7 +155,7 @@ class LocalDirFetcher(object):
class SystemPackageFetcher(object):
def __init__(self, build_options, packages):
def __init__(self, build_options, packages) -> None:
self.manager = build_options.host_type.get_package_manager()
self.packages = packages.get(self.manager)
self.host_type = build_options.host_type
@@ -190,29 +190,29 @@ class SystemPackageFetcher(object):
return bool(self.installed)
def update(self):
def update(self) -> ChangeStatus:
assert self.installed
return ChangeStatus(all_changed=False)
def hash(self):
def hash(self) -> str:
if self.packages_are_installed():
return hashlib.sha256(self.installed).hexdigest()
else:
return "0" * 40
def get_src_dir(self):
def get_src_dir(self) -> None:
return None
class PreinstalledNopFetcher(SystemPackageFetcher):
def __init__(self):
def __init__(self) -> None:
self.installed = True
class GitFetcher(Fetcher):
DEFAULT_DEPTH = 1
def __init__(self, build_options, manifest, repo_url, rev, depth):
def __init__(self, build_options, manifest, repo_url, rev, depth) -> None:
# Extract the host/path portions of the URL and generate a flattened
# directory name. eg:
# github.com/facebook/folly.git -> github.com-facebook-folly.git
@@ -246,7 +246,7 @@ class GitFetcher(Fetcher):
self.manifest = manifest
self.depth = depth if depth else GitFetcher.DEFAULT_DEPTH
def _update(self):
def _update(self) -> ChangeStatus:
current_hash = (
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=self.repo_dir)
.strip()
@@ -281,7 +281,7 @@ class GitFetcher(Fetcher):
self._clone()
return ChangeStatus(True)
def _clone(self):
def _clone(self) -> None:
print("Cloning %s..." % self.origin_repo)
# The basename/dirname stuff allows us to dance around issues where
# eg: this python process is native win32, but the git.exe is cygwin
@@ -300,7 +300,7 @@ class GitFetcher(Fetcher):
)
self._update()
def clean(self):
def clean(self) -> None:
if os.path.exists(self.repo_dir):
run_cmd(["git", "clean", "-fxd"], cwd=self.repo_dir)
@@ -343,7 +343,7 @@ def does_file_need_update(src_name, src_st, dest_name):
return False
def copy_if_different(src_name, dest_name):
def copy_if_different(src_name, dest_name) -> bool:
"""Copy src_name -> dest_name, but only touch dest_name
if src_name is different from dest_name, making this a
more build system friendly way to copy."""
@@ -380,22 +380,22 @@ def list_files_under_dir_newer_than_timestamp(dir_to_scan, ts):
class ShipitPathMap(object):
def __init__(self):
def __init__(self) -> None:
self.roots = []
self.mapping = []
self.exclusion = []
def add_mapping(self, fbsource_dir, target_dir):
def add_mapping(self, fbsource_dir, target_dir) -> None:
"""Add a posix path or pattern. We cannot normpath the input
here because that would change the paths from posix to windows
form and break the logic throughout this class."""
self.roots.append(fbsource_dir)
self.mapping.append((fbsource_dir, target_dir))
def add_exclusion(self, pattern):
def add_exclusion(self, pattern) -> None:
self.exclusion.append(re.compile(pattern))
def _minimize_roots(self):
def _minimize_roots(self) -> None:
"""compute the de-duplicated set of roots within fbsource.
We take the shortest common directory prefix to make this
determination"""
@@ -413,7 +413,7 @@ class ShipitPathMap(object):
self.roots = minimized
def _sort_mapping(self):
def _sort_mapping(self) -> None:
self.mapping.sort(reverse=True, key=lambda x: len(x[0]))
def _map_name(self, norm_name, dest_root):
@@ -442,7 +442,7 @@ class ShipitPathMap(object):
raise Exception("%s did not match any rules" % norm_name)
def mirror(self, fbsource_root, dest_root):
def mirror(self, fbsource_root, dest_root) -> ChangeStatus:
self._minimize_roots()
self._sort_mapping()
@@ -516,7 +516,7 @@ class FbsourceRepoData(NamedTuple):
FBSOURCE_REPO_DATA: Dict[str, FbsourceRepoData] = {}
def get_fbsource_repo_data(build_options):
def get_fbsource_repo_data(build_options) -> FbsourceRepoData:
"""Returns the commit metadata for the fbsource repo.
Since we may have multiple first party projects to
hash, and because we don't mutate the repo, we cache
@@ -546,13 +546,13 @@ def get_fbsource_repo_data(build_options):
class SimpleShipitTransformerFetcher(Fetcher):
def __init__(self, build_options, manifest, ctx):
def __init__(self, build_options, manifest, ctx) -> None:
self.build_options = build_options
self.manifest = manifest
self.repo_dir = os.path.join(build_options.scratch_dir, "shipit", manifest.name)
self.ctx = ctx
def clean(self):
def clean(self) -> None:
if os.path.exists(self.repo_dir):
shutil.rmtree(self.repo_dir)
@@ -571,7 +571,8 @@ class SimpleShipitTransformerFetcher(Fetcher):
return mapping.mirror(self.build_options.fbsource_dir, self.repo_dir)
def hash(self):
# pyre-fixme[15]: `hash` overrides method defined in `Fetcher` inconsistently.
def hash(self) -> str:
# We return a fixed non-hash string for in-fbsource builds.
# We're relying on the `update` logic to correctly invalidate
# the build in the case that files have changed.
@@ -584,18 +585,18 @@ class SimpleShipitTransformerFetcher(Fetcher):
class ShipitTransformerFetcher(Fetcher):
SHIPIT = "/var/www/scripts/opensource/shipit/run_shipit.php"
def __init__(self, build_options, project_name):
def __init__(self, build_options, project_name) -> None:
self.build_options = build_options
self.project_name = project_name
self.repo_dir = os.path.join(build_options.scratch_dir, "shipit", project_name)
def update(self):
def update(self) -> ChangeStatus:
if os.path.exists(self.repo_dir):
return ChangeStatus()
self.run_shipit()
return ChangeStatus(True)
def clean(self):
def clean(self) -> None:
if os.path.exists(self.repo_dir):
shutil.rmtree(self.repo_dir)
@@ -603,7 +604,7 @@ class ShipitTransformerFetcher(Fetcher):
def available(cls):
return os.path.exists(cls.SHIPIT)
def run_shipit(self):
def run_shipit(self) -> None:
tmp_path = self.repo_dir + ".new"
try:
if os.path.exists(tmp_path):
@@ -640,7 +641,8 @@ class ShipitTransformerFetcher(Fetcher):
self.clean()
raise
def hash(self):
# pyre-fixme[15]: `hash` overrides method defined in `Fetcher` inconsistently.
def hash(self) -> str:
# We return a fixed non-hash string for in-fbsource builds.
return "fbsource"
@@ -648,7 +650,7 @@ class ShipitTransformerFetcher(Fetcher):
return self.repo_dir
def download_url_to_file_with_progress(url, file_name):
def download_url_to_file_with_progress(url: str, file_name) -> None:
print("Download %s -> %s ..." % (url, file_name))
class Progress(object):
@@ -686,7 +688,7 @@ def download_url_to_file_with_progress(url, file_name):
class ArchiveFetcher(Fetcher):
def __init__(self, build_options, manifest, url, sha256):
def __init__(self, build_options, manifest, url, sha256) -> None:
self.manifest = manifest
self.url = url
self.sha256 = sha256
@@ -698,7 +700,7 @@ class ArchiveFetcher(Fetcher):
self.src_dir = os.path.join(build_options.scratch_dir, "extracted", basename)
self.hash_file = self.src_dir + ".hash"
def _verify_hash(self):
def _verify_hash(self) -> None:
h = hashlib.sha256()
with open(self.file_name, "rb") as f:
while True:
@@ -720,16 +722,16 @@ class ArchiveFetcher(Fetcher):
os.makedirs(download_dir)
return download_dir
def _download(self):
def _download(self) -> None:
self._download_dir()
download_url_to_file_with_progress(self.url, self.file_name)
self._verify_hash()
def clean(self):
def clean(self) -> None:
if os.path.exists(self.src_dir):
shutil.rmtree(self.src_dir)
def update(self):
def update(self) -> ChangeStatus:
try:
with open(self.hash_file, "r") as f:
saved_hash = f.read().strip()