Skip to content

Commit 1b6150d

Browse files
authored
package and deliver parser as a library (#114)
* package and deliver parser as a library * bye arrow * apply black formatting
1 parent a59ea34 commit 1b6150d

File tree

8 files changed

+83
-22
lines changed

8 files changed

+83
-22
lines changed

.github/workflows/release.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Publish to PyPI
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
pypi-publish:
10+
name: upload release to PyPI
11+
runs-on: ubuntu-latest
12+
environment: release
13+
permissions:
14+
id-token: write
15+
steps:
16+
- uses: actions/checkout@v3
17+
- uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.11'
20+
- name: Install pypa/build
21+
run: >-
22+
python3 -m
23+
pip install
24+
build
25+
--user
26+
- name: Build a binary wheel and a source tarball
27+
run: >-
28+
python3 -m
29+
build
30+
--sdist
31+
--wheel
32+
--outdir dist/
33+
.
34+
- name: Publish package distributions to PyPI
35+
uses: pypa/gh-action-pypi-publish@release/v1

linehaul/events/parser.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import logging
1515
import posixpath
1616

17+
from datetime import datetime, timezone
1718
from typing import Optional
1819

19-
import arrow
2020
import attr
2121
import attr.validators
2222
import cattr
@@ -33,7 +33,10 @@
3333

3434
_cattr = cattr.Converter()
3535
_cattr.register_structure_hook(
36-
arrow.Arrow, lambda d, t: arrow.get(d[5:-4], "DD MMM YYYY HH:mm:ss")
36+
datetime,
37+
lambda d, t: datetime.strptime(d[5:-4], "%d %b %Y %H:%M:%S").replace(
38+
tzinfo=timezone.utc
39+
),
3740
)
3841

3942

@@ -145,7 +148,6 @@ class PackageType(enum.Enum):
145148

146149
@attr.s(slots=True, frozen=True)
147150
class File:
148-
149151
filename = attr.ib(validator=attr.validators.instance_of(str))
150152
project = attr.ib(validator=attr.validators.instance_of(str))
151153
version = attr.ib(validator=attr.validators.instance_of(str))
@@ -154,8 +156,7 @@ class File:
154156

155157
@attr.s(slots=True, frozen=True)
156158
class Download:
157-
158-
timestamp = attr.ib(type=arrow.Arrow)
159+
timestamp = attr.ib(type=datetime)
159160
url = attr.ib(validator=attr.validators.instance_of(str))
160161
project = attr.ib(validator=attr.validators.instance_of(str))
161162
file = attr.ib(type=File)
@@ -176,8 +177,7 @@ class Download:
176177

177178
@attr.s(slots=True, frozen=True)
178179
class Simple:
179-
180-
timestamp = attr.ib(type=arrow.Arrow)
180+
timestamp = attr.ib(type=datetime)
181181
url = attr.ib(validator=attr.validators.instance_of(str))
182182
project = attr.ib(validator=attr.validators.instance_of(str))
183183
tls_protocol = attr.ib(

linehaul/ua/datastructures.py

-6
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,24 @@
1717

1818
@attr.s(slots=True, frozen=True)
1919
class Installer:
20-
2120
name = attr.ib(type=Optional[str], default=None)
2221
version = attr.ib(type=Optional[str], default=None)
2322

2423

2524
@attr.s(slots=True, frozen=True)
2625
class Implementation:
27-
2826
name = attr.ib(type=Optional[str], default=None)
2927
version = attr.ib(type=Optional[str], default=None)
3028

3129

3230
@attr.s(slots=True, frozen=True)
3331
class LibC:
34-
3532
lib = attr.ib(type=Optional[str], default=None)
3633
version = attr.ib(type=Optional[str], default=None)
3734

3835

3936
@attr.s(slots=True, frozen=True)
4037
class Distro:
41-
4238
name = attr.ib(type=Optional[str], default=None)
4339
version = attr.ib(type=Optional[str], default=None)
4440
id = attr.ib(type=Optional[str], default=None)
@@ -47,14 +43,12 @@ class Distro:
4743

4844
@attr.s(slots=True, frozen=True)
4945
class System:
50-
5146
name = attr.ib(type=Optional[str], default=None)
5247
release = attr.ib(type=Optional[str], default=None)
5348

5449

5550
@attr.s(slots=True, frozen=True)
5651
class UserAgent:
57-
5852
installer = attr.ib(type=Optional[Installer], default=None)
5953
python = attr.ib(type=Optional[str], default=None)
6054
implementation = attr.ib(type=Optional[Implementation], default=None)

main.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import arrow
21
import cattr
32

43
import base64
@@ -18,7 +17,7 @@
1817

1918
_cattr = cattr.Converter()
2019
_cattr.register_unstructure_hook(
21-
arrow.Arrow, lambda o: o.format("YYYY-MM-DD HH:mm:ss ZZ")
20+
datetime.datetime, lambda o: o.strftime("%Y-%m-%d %H:%M:%S +00:00")
2221
)
2322

2423
DEFAULT_PROJECT = os.environ.get("GCP_PROJECT", "the-psf")
@@ -64,7 +63,7 @@ def process_fastly_log(data, context):
6463
simple_results_file = stack.enter_context(NamedTemporaryFile())
6564
download_results_file = stack.enter_context(NamedTemporaryFile())
6665

67-
min_timestamp = arrow.utcnow()
66+
min_timestamp = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
6867
for line in input_file:
6968
try:
7069
res = parse(line.decode())

pyproject.toml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[project]
2+
name = "linehaul"
3+
version = "1.0.0"
4+
description = "User-Agent parsing for PyPI analytics"
5+
6+
readme = "README.md"
7+
requires-python = ">=3.11, <4"
8+
license = {file = "LICENSE.txt"}
9+
10+
keywords = ["pypi", "user-agent", "parsing"]
11+
authors = [
12+
{name = "PyPI Admins", email = "[email protected]" }
13+
]
14+
15+
classifiers = [
16+
"Development Status :: 5 - Production/Stable",
17+
"Intended Audience :: Developers",
18+
"Topic :: Software Development :: Build Tools",
19+
"License :: OSI Approved :: Apache Software License",
20+
"Programming Language :: Python :: 3",
21+
"Programming Language :: Python :: 3.11",
22+
"Programming Language :: Python :: 3 :: Only",
23+
]
24+
25+
dependencies = [
26+
"cattrs",
27+
"packaging",
28+
"pyparsing",
29+
]
30+
31+
[project.urls]
32+
"Homepage" = "https://github.com/pypi/linehaul-cloud-function"
33+
"Source" = "https://github.com/pypi/linehaul-cloud-function"
34+
35+
[build-system]
36+
requires = ["setuptools>=43.0.0", "wheel"]
37+
build-backend = "setuptools.build_meta"

requirements.in

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
arrow
21
cattrs
32
packaging
43
pyparsing

requirements.txt

-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#
55
# pip-compile --allow-unsafe --generate-hashes --output-file=requirements.txt requirements.in
66
#
7-
arrow==1.2.3 \
8-
--hash=sha256:3934b30ca1b9f292376d9db15b19446088d12ec58629bc3f0da28fd55fb633a1 \
9-
--hash=sha256:5a49ab92e3b7b71d96cd6bfcc4df14efefc9dfa96ea19045815914a6ab6b1fe2
10-
# via -r requirements.in
117
attrs==22.2.0 \
128
--hash=sha256:29e95c7f6778868dbd49170f98f8818f78f3dc5e0e37c0b1f474e3561b240836 \
139
--hash=sha256:c9227bfc2f01993c03f68db37d1d15c9690188323c067c641f1a35ca58185f99
@@ -348,7 +344,6 @@ python-dateutil==2.8.2 \
348344
--hash=sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86 \
349345
--hash=sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9
350346
# via
351-
# arrow
352347
# google-cloud-bigquery
353348
requests==2.28.2 \
354349
--hash=sha256:64299f4909223da747622c030b781c0d7811e359c37124b4bd368fb8c6518baa \

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[options]
2+
packages = find:

0 commit comments

Comments
 (0)