Skip to content

Commit

Permalink
android: add maven upload script (#459)
Browse files Browse the repository at this point in the history
```
export READWRITE_USER="usr";
export READWRITE_API_KEY="psswrd";
export ARTIFACT_HOST_URL="foo.api.com";
python bazel/upload_maven.py --version=LOCAL-SNAPSHOT-1.0.0
```

Signed-off-by: Alan Chiu <[email protected]>

For an explanation of how to fill out the fields, please see the relevant section
in [PULL_REQUESTS.md](https://github.com/envoyproxy/envoy/blob/master/PULL_REQUESTS.md)

Description: add a pythons script for maven upload and local development
Risk Level: low
Testing: to private artifactory
Docs Changes: n/a
Release Notes: n/a
[Optional Fixes #Issue]
[Optional Deprecated:]

Signed-off-by: JP Simard <[email protected]>
  • Loading branch information
Alan Chiu authored and jpsim committed Nov 28, 2022
1 parent d90d03c commit ba36f5c
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 7 deletions.
1 change: 1 addition & 0 deletions mobile/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ build \
--verbose_failures \
--workspace_status_command=envoy/bazel/get_workspace_status \
--xcode_version=11.0.0 \
--incompatible_depset_is_not_iterable=false \
--incompatible_bzl_disallow_load_after_statement=false \
--javabase=@bazel_tools//tools/jdk:jdk

Expand Down
4 changes: 3 additions & 1 deletion mobile/.gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/bazel-*
/build_*
/dist/envoy.aar
/dist/*pom.xml
/dist/*.aar
/dist/Envoy.framework
.DS_Store
/generated
.idea
.ijwb
.aswb
*.pyc
tags
/tmp
Expand Down
16 changes: 13 additions & 3 deletions mobile/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@ touch $@
stamp = True,
)

alias(
name = "android_pom",
actual = "//library/kotlin/src/io/envoyproxy/envoymobile:android_aar_pom",
)

alias(
name = "android_aar",
actual = "//library/kotlin/src/io/envoyproxy/envoymobile:android_aar",
)

genrule(
name = "android_dist",
srcs = ["android_aar"],
outs = ["android_out"],
srcs = [
"android_aar",
"android_pom",
],
outs = ["stub_android_dist_output"],
cmd = """
cp $< dist/envoy.aar
cp $(location :android_aar) dist/envoy.aar
cp $(location :android_pom) dist/envoy-pom.xml
chmod 755 dist/envoy.aar
chmod 755 dist/envoy-pom.xml
touch $@
""",
stamp = True,
Expand Down
10 changes: 10 additions & 0 deletions mobile/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
# Kotlin
"org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.11",

# Test artifacts
"org.assertj:assertj-core:3.9.0",
"junit:junit:4.12",
Expand All @@ -121,6 +124,13 @@ maven_install(
],
)

http_archive(
name = "google_bazel_common",
strip_prefix = "bazel-common-f1115e0f777f08c3cdb115526c4e663005bec69b",
urls = ["https://github.com/google/bazel-common/archive/f1115e0f777f08c3cdb115526c4e663005bec69b.zip"],
sha256 = "1e05a4791cc3470d3ecf7edb556f796b1d340359f1c4d293f175d4d0946cf84c",
)

http_archive(
name = "io_bazel_rules_kotlin",
sha256 = "52f88499cdd7db892a500951ea5cbb749245c5635e6da0b80a3b7ad4ea976f31",
Expand Down
4 changes: 4 additions & 0 deletions mobile/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ kt_jvm_library(
"@maven//:junit_junit",
],
)

exports_files([
"pom_template.xml",
])
13 changes: 11 additions & 2 deletions mobile/bazel/aar_with_jni.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@google_bazel_common//tools/maven:pom_file.bzl", "pom_file")

# This file is based on https://github.com/aj-michael/aar_with_jni which is
# subject to the following copyright and license:

Expand Down Expand Up @@ -53,10 +55,18 @@ EOF
deps = [android_library],
)

pom_file(
name = name + "_pom",
targets = [android_library],
template_file = "//bazel:pom_template.xml",
visibility = ["//visibility:public"],
)

native.genrule(
name = name,
srcs = [android_library + "_kt.jar", android_library + ".aar", archive_name + "_jni_unsigned.apk", proguard_rules],
srcs = [android_library + "_kt.jar", android_library + ".aar", archive_name + "_jni_unsigned.apk", name + "_pom.xml", proguard_rules],
outs = [archive_name + ".aar"],
visibility = visibility,
cmd = """
cp $(location {proguard_rules}) ./proguard.txt
cp $(location {android_library}.aar) $(location :{archive_name}.aar)
Expand All @@ -69,5 +79,4 @@ cp $$origdir/$(location {android_library}_kt.jar) classes.jar
cp $$origdir/proguard.txt .
zip -r $$origdir/$(location :{archive_name}.aar) jni/*/*.so classes.jar proguard.txt
""".format(android_library = android_library, archive_name = archive_name, proguard_rules = proguard_rules),
visibility = visibility,
)
2 changes: 1 addition & 1 deletion mobile/bazel/kotlin_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def envoy_mobile_kt_aar_android_library(name, custom_package, manifest, visibili
custom_package = custom_package,
manifest = manifest,
visibility = ["//visibility:public"],
deps = deps,
deps = deps + ["@maven//:org_jetbrains_kotlin_kotlin_stdlib_jdk8"],
)

def envoy_mobile_android_library(name, custom_package, manifest, visibility = None, srcs = [], deps = []):
Expand Down
14 changes: 14 additions & 0 deletions mobile/bazel/pom_template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<groupId>io.envoyproxy.envoymobile</groupId>
<artifactId>envoy</artifactId>
<version>{pom_version}</version>
<packaging>aar</packaging>
<dependencies>
{generated_bzl_deps}
</dependencies>
</project>
137 changes: 137 additions & 0 deletions mobile/dist/upload_maven.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/env python

from __future__ import print_function

import argparse
import base64
import os
import shutil
import time

try:
from urllib.request import urlopen, Request, HTTPError
except ImportError: # python 2
from urllib2 import urlopen, Request, HTTPError

_USER_CREDS = os.environ.get("READWRITE_USER", "")
_KEY_CREDS = os.environ.get("READWRITE_API_KEY", "")
_ARTIFACT_HOST_URL = os.environ["ARTIFACT_HOST_URL"]

_GROUP_ID = "io.envoyproxy.envoymobile"
_ARTIFACT_ID = "envoy"
_BASE_URL = "{}/io/envoyproxy/envoymobile".format(_ARTIFACT_HOST_URL)
_LOCAL_INSTALL_PATH = os.path.expanduser("~/.m2/repository/io/envoyproxy/envoymobile/{}".format(_ARTIFACT_ID))


def _upload(target, version):
aar = target + ".aar"
source_jar = target + "-sources.jar" # TODO: figure out how to generate sources jar within bazel
pom_file = target + "-pom.xml"

files = [
(aar, "{}-{}.aar".format(_ARTIFACT_ID, version)),
(source_jar, "{}-{}-sources.jar".format(_ARTIFACT_ID, version)),
(pom_file, "{}-{}.pom".format(_ARTIFACT_ID, version))
]

if version == "LOCAL-SNAPSHOT":
_install_locally(version, files)
else:
_upload_to_artifactory(version, files)


def _upload_to_artifactory(version, files):
aar = files[0][0]
artifact_name = os.path.splitext(os.path.basename(aar))[0]

package_url = os.path.join(_BASE_URL, artifact_name, version)
base64string = base64.b64encode("{}:{}".format(_USER_CREDS,
_KEY_CREDS)
.encode()).decode()

for src_path, target_path in files:
# Temporarily allow missing source jars
if not os.path.exists(src_path):
continue
basename = os.path.basename(target_path)
artifact_url = os.path.join(package_url, basename)
try:
with open(src_path, "rb") as f:
request = Request(artifact_url, f.read())

request.add_header("Authorization", "Basic {}".format(base64string))
request.get_method = lambda: "PUT"
urlopen(request)
except HTTPError as e:
if e.code == 403:
print("Ignoring duplicate upload for {}".format(artifact_url))
else:
print("Exception raised for {}".format(src_path))
print(e.headers)
raise
except Exception as e:
print(e)
raise


def _install_locally(version, files):
path = "{}/{}".format(_LOCAL_INSTALL_PATH, version)
if os.path.exists(path):
shutil.rmtree(path)

os.makedirs(path)

f = open("{}/maven-metadata-local.xml".format(path), 'w+')
f.write("""\
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>{group_id}</groupId>
<artifactId>{artifact_id}</artifactId>
<versioning>
<release>{version}</release>
<versions>
<version>{version}</version>
</versions>
<lastUpdated>{timestamp}</lastUpdated>
</versioning>
</metadata>
""".format(version=version,
timestamp=int(round(time.time() * 1000)),
group_id=_GROUP_ID,
artifact_id=_ARTIFACT_ID))
f.close()

for src_path, target_path in files:
# Temporarily allow missing source jars
if not os.path.exists(src_path):
continue

basename = os.path.basename(target_path)
shutil.copyfile(src_path, os.path.join(path, basename))


def _build_parser():
parser = argparse.ArgumentParser()
parser.add_argument("--version", default="LOCAL-SNAPSHOT")
return parser


if __name__ == "__main__":
args = _build_parser().parse_args()

aar = None
pom = None

# Assume the bazel rule will nicely put the aar and pom in the dist/ directory
for root, _, files in os.walk("dist"):
for filepath in files:
if filepath.endswith("{}-pom.xml".format(_ARTIFACT_ID)):
pom = os.path.join(root, filepath)
elif filepath.endswith("{}.aar".format(_ARTIFACT_ID)):
aar = os.path.join(root, filepath)

if pom is None or aar is None:
# TODO: raise exception?
print("Unable to find pom.xml and aar in dist/")

_upload(aar[: -len(".aar")], args.version)

0 comments on commit ba36f5c

Please sign in to comment.