Пример #1
0
    def _DoExport():
        source_workspace = phd_workspace.PhdWorkspace(workspace_root)

        with tempfile.TemporaryDirectory(
                prefix=f'phd_export_{github_repo}_') as d:
            destination = pathlib.Path(d)
            credentials = api.ReadGitHubCredentials(
                pathlib.Path('~/.githubrc').expanduser())
            connection = github_lib.Github(credentials.username,
                                           credentials.password)
            repo = GetOrCreateRepoOrDie(connection, github_repo)
            api.CloneRepoToDestination(repo, destination)
            destination_repo = git.Repo(destination)

            src_files = source_workspace.GetAllSourceTreeFiles(
                targets, excluded_targets, extra_files, move_file_mapping)
            if FLAGS.export_source_tree_print_files:
                print('\n'.join(src_files))
                sys.exit(0)

            exported_commit_count = source_workspace.ExportToRepo(
                repo=destination_repo,
                targets=targets,
                src_files=src_files,
                extra_files=extra_files,
                file_move_mapping=move_file_mapping,
                resume_export=resume_export)
            if not exported_commit_count:
                return
            app.Log(1, 'Pushing changes to remote')
            destination_repo.git.push('origin')
  def _DoExport():
    source_path = pathlib.Path(getconfig.GetGlobalConfig().paths.repo_root)
    source_workspace = phd_workspace.PhdWorkspace(source_path)

    with tempfile.TemporaryDirectory(prefix='phd_tools_source_tree_') as d:
      destination = pathlib.Path(d)
      credentials = api.ReadGitHubCredentials(
          pathlib.Path('~/.githubrc').expanduser())
      connection = github_lib.Github(credentials.username, credentials.password)
      repo = GetOrCreateRepoOrDie(connection, github_repo)
      api.CloneRepoToDestination(repo, destination)
      destination_repo = git.Repo(destination)

      src_files = source_workspace.GetAllSourceTreeFiles(
          targets, excluded_targets, extra_files, move_file_mapping)
      if FLAGS.export_source_tree_print_files:
        print('\n'.join(src_files))
        sys.exit(0)

      source_workspace.ExportToRepo(destination_repo, targets, src_files,
                                    extra_files, move_file_mapping)
      app.Log(1, 'Pushing changes to remote')
      destination_repo.git.push('origin')
Пример #3
0
def Export(
  workspace_root: pathlib.Path,
  github_repo: str,
  targets: List[str],
  excluded_targets: List[str] = None,
  extra_files: List[str] = None,
  move_file_mapping: Dict[str, str] = None,
) -> None:
  """Custom entry-point to export source-tree.

  This should be called from a bare python script, before flags parsing.

  Args:
    workspace_root: The root path of the bazel workspace.
    github_repo: The name of the GitHub repo to export to.
    targets: A list of bazel targets to export. These targets, and their
      dependencies, will be exported. These arguments are passed unmodified to
      bazel query, so `/...` and `:all` labels are expanded, e.g.
      `//some/package/to/export/...`. All targets should be absolute, and
      prefixed with '//'.
    excluded_targets: A list of targets to exlude.
    extra_files: A list of additional files to export.
    move_file_mapping: A dictionary of <src,dst> relative paths listing files
      which should be moved from their respective source location to the
      destination.
  """
  excluded_targets = excluded_targets or []
  extra_files = extra_files or []
  move_file_mapping = move_file_mapping or {}

  source_workspace = phd_workspace.PhdWorkspace(workspace_root)

  with tempfile.TemporaryDirectory(prefix=f"phd_export_{github_repo}_") as d:
    destination = pathlib.Path(d)
    connection = api.GetDefaultGithubConnectionOrDie(
      extra_access_token_paths=[
        "~/.github/access_tokens/export_source_tree.txt"
      ]
    )
    repo = GetOrCreateRepoOrDie(connection, github_repo)
    api.CloneRepo(repo, destination)
    destination_repo = git.Repo(destination)

    src_files = source_workspace.GetAllSourceTreeFiles(
      targets, excluded_targets, extra_files, move_file_mapping
    )
    if FLAGS.export_source_tree_print_files:
      print("\n".join(str(x) for x in src_files))
      sys.exit(0)

    exported_commit_count = source_workspace.ExportToRepo(
      repo=destination_repo,
      targets=targets,
      src_files=src_files,
      extra_files=extra_files,
      file_move_mapping=move_file_mapping,
    )
    if not exported_commit_count:
      return
    app.Log(1, "Pushing changes to remote")

    # Force push since we may have rewritten history.
    destination_repo.git.push("origin", force=True)
Пример #4
0
def _DoDeployPip(
  package_name: str,
  package_root: str,
  classifiers: typing.List[str],
  description: str,
  keywords: typing.List[str],
  license: str,
  long_description_file: str,
  url: typing.Optional[str],
):
  """Private implementation function."""
  release_type = FLAGS.release_type
  if release_type not in {"release", "testing"}:
    raise OSError("--release_type must be one of: {testing,release}")

  source_path = pathlib.Path(workspace_status.STABLE_UNSAFE_WORKSPACE)
  workspace = phd_workspace.PhdWorkspace(source_path)

  if url is None:
    url = GetUrlFromCnameFile(workspace, package_root)

  with DeploymentDirectory(workspace) as deployment_package:
    app.Log(1, "Assembling package ...")
    targets = FindPyLibraries(workspace, package_root)

    requirements = workspace.GetPythonRequirementsForTarget(targets)
    requirements = [r.split()[0] for r in requirements]

    def QuotedList(items, indent: int = 8):
      """Produce an indented, quoted list."""
      prefix = " " * indent
      list = f",\n{prefix}".join([f'"{x}"' for x in items])
      return f"{prefix}{list},"

    rules = f"""
load(
    "@graknlabs_bazel_distribution//pip:rules.bzl",
    "assemble_pip",
    "deploy_pip"
)

py_library(
    name = "{package_name}",
    deps = [
{QuotedList(targets)}
    ],
)

assemble_pip(
    name = "package",
    author = "Chris Cummins",
    author_email ="*****@*****.**",
    classifiers=[
{QuotedList(classifiers)}
    ],
    description="{description}",
    install_requires=[
{QuotedList(requirements)}
    ],
    keywords=[
{QuotedList(keywords)}
    ],
    license="{license}",
    long_description_file="{long_description_file}",
    package_name="{package_name}",
    target=":{package_name}",
    url="{url}",
    version_file="//:version.txt",
)

deploy_pip(
  name = "deploy",
  deployment_properties = "//:deployment.properties",
  target = ":package",
)
"""
    app.Log(2, "Generated rules:\n%s", rules)

    fs.Write(
      workspace.workspace_root / deployment_package / "BUILD",
      rules.encode("utf-8"),
    )

    # Build and deploy separately, to make it easier to diagnose the cause of
    # any error.
    app.Log(1, "Building package ...")
    build = workspace.Bazel("build", [f"//{deployment_package}:package"])
    build.communicate()
    if build.returncode:
      raise OSError("package build failed")

    app.Log(1, "Deploying package ...")
    build = workspace.Bazel(
      "run", [f"//{deployment_package}:deploy", "--", release_type],
    )
    build.communicate()
    if build.returncode:
      raise OSError("deployment failed")