Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
bcecb0f7
Commit
bcecb0f7
authored
Oct 21, 2020
by
Mathieu Parent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Packages::Debian::ExtractMetadataService
parent
e3a57b32
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
144 additions
and
0 deletions
+144
-0
app/services/packages/debian/extract_metadata_service.rb
app/services/packages/debian/extract_metadata_service.rb
+85
-0
spec/services/packages/debian/extract_metadata_service_spec.rb
...services/packages/debian/extract_metadata_service_spec.rb
+59
-0
No files found.
app/services/packages/debian/extract_metadata_service.rb
0 → 100644
View file @
bcecb0f7
# frozen_string_literal: true
module
Packages
module
Debian
class
ExtractMetadataService
include
Gitlab
::
Utils
::
StrongMemoize
ExtractionError
=
Class
.
new
(
StandardError
)
def
initialize
(
package_file
)
@package_file
=
package_file
end
def
execute
raise
ExtractionError
.
new
(
'invalid package file'
)
unless
valid_package_file?
extract_metadata
end
private
attr_reader
:package_file
def
valid_package_file?
package_file
&&
package_file
.
package
&
.
debian?
&&
package_file
.
file
.
size
>
0
# rubocop:disable Style/ZeroLengthPredicate
end
def
file_type_basic
%i[dsc deb udeb buildinfo changes]
.
each
do
|
format
|
return
format
if
package_file
.
file_name
.
end_with?
(
".
#{
format
}
"
)
end
nil
end
def
file_type_source
# https://manpages.debian.org/buster/dpkg-dev/dpkg-source.1.en.html
%i[gzip bzip2 lzma xz]
.
each
do
|
format
|
return
:source
if
package_file
.
file_name
.
end_with?
(
".tar.
#{
format
}
"
)
end
nil
end
def
file_type
strong_memoize
(
:file_type
)
do
file_type_basic
||
file_type_source
||
:unknown
end
end
def
file_type_debian?
file_type
==
:deb
||
file_type
==
:udeb
end
def
file_type_meta?
file_type
==
:dsc
||
file_type
==
:buildinfo
||
file_type
==
:changes
end
def
extracted_fields
if
file_type_debian?
package_file
.
file
.
use_file
do
|
file_path
|
::
Packages
::
Debian
::
ExtractDebMetadataService
.
new
(
file_path
).
execute
end
elsif
file_type_meta?
package_file
.
file
.
use_file
do
|
file_path
|
::
Packages
::
Debian
::
ParseDebian822Service
.
new
(
File
.
read
(
file_path
)).
execute
.
each_value
.
first
end
end
end
def
extract_metadata
fields
=
extracted_fields
architecture
=
fields
.
delete
(
:Architecture
)
if
file_type_debian?
{
file_type:
file_type
,
architecture:
architecture
,
fields:
fields
}
end
end
end
end
spec/services/packages/debian/extract_metadata_service_spec.rb
0 → 100644
View file @
bcecb0f7
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
Packages
::
Debian
::
ExtractMetadataService
do
let
(
:service
)
{
described_class
.
new
(
package_file
)
}
subject
{
service
.
execute
}
RSpec
.
shared_context
'Debian ExtractMetadata Service'
do
|
trait
|
let
(
:package_file
)
{
create
(
:debian_package_file
,
trait
)
}
end
RSpec
.
shared_examples
'Test Debian ExtractMetadata Service'
do
|
expected_file_type
,
expected_architecture
,
expected_fields
|
it
"returns file_type
#{
expected_file_type
.
inspect
}
"
do
expect
(
subject
[
:file_type
]).
to
eq
(
expected_file_type
)
end
it
"returns architecture
#{
expected_architecture
.
inspect
}
"
do
expect
(
subject
[
:architecture
]).
to
eq
(
expected_architecture
)
end
it
"returns fields
#{
expected_fields
.
nil?
?
''
:
'including '
}#{
expected_fields
.
inspect
}
"
do
if
expected_fields
.
nil?
expect
(
subject
[
:fields
]).
to
be_nil
else
expect
(
subject
[
:fields
]).
to
include
(
**
expected_fields
)
end
end
end
using
RSpec
::
Parameterized
::
TableSyntax
where
(
:case_name
,
:trait
,
:expected_file_type
,
:expected_architecture
,
:expected_fields
)
do
'with invalid'
|
:invalid
|
:unknown
|
nil
|
nil
'with source'
|
:source
|
:source
|
nil
|
nil
'with dsc'
|
:dsc
|
:dsc
|
nil
|
{
'Binary'
:
'sample-dev, libsample0, sample-udeb'
}
'with deb'
|
:deb
|
:deb
|
'amd64'
|
{
'Multi-Arch'
:
'same'
}
'with udeb'
|
:udeb
|
:udeb
|
'amd64'
|
{
'Package'
:
'sample-udeb'
}
'with buildinfo'
|
:buildinfo
|
:buildinfo
|
nil
|
{
'Architecture'
:
'amd64 source'
,
'Build-Architecture'
:
'amd64'
}
'with changes'
|
:changes
|
:changes
|
nil
|
{
'Architecture'
:
'source amd64'
,
'Binary'
:
'libsample0 sample-dev sample-udeb'
}
end
with_them
do
include_context
'Debian ExtractMetadata Service'
,
params
[
:trait
]
do
it_behaves_like
'Test Debian ExtractMetadata Service'
,
params
[
:expected_file_type
],
params
[
:expected_architecture
],
params
[
:expected_fields
]
end
end
context
'with invalid package file'
do
let
(
:package_file
)
{
create
(
:conan_package_file
)
}
it
'raise error'
do
expect
{
subject
}.
to
raise_error
(
described_class
::
ExtractionError
,
'invalid package file'
)
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment